package com.x.erp.manage;

import com.x.erp.dao.*;
import com.x.erp.model.*;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.framework.manage.BaseManage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MultilevelActivityBuyManage extends BaseManage{

	@Autowired
	private MultilevelActivityBuyDao multilevelActivityBuyDao;// 多级营销活动购买管理

	@Autowired
	private MultilevelActivityShareDao multilevelActivityShareDao;// 多级营销活动分享管理

	@Autowired
	private MultilevelActivityDao multilevelActivityDao;// 多级营销活动管理

	@Autowired
	private MultilevelActivityRewardDao multilevelActivityRewardDao;// 多级营销活动奖励管理

	@Autowired
	private MultilevelActivityAccountDao multilevelActivityAccountDao;// 多级营销活动用户账户管理

	public MultilevelActivityBuy insertMultilevelActivityBuy(MultilevelActivityBuy multilevelActivityBuy) throws Exception {
		MultilevelActivityBuy buy = new MultilevelActivityBuy();
		buy.setUserId(multilevelActivityBuy.getUserId());
		buy.setActivityId(multilevelActivityBuy.getActivityId());
		List<MultilevelActivityBuy> shares = multilevelActivityBuyDao.selectMultilevelActivityBuyList(buy);
		if(!shares.isEmpty()){
			return null;
		}
		return this.insert(multilevelActivityBuy);
	}

	public ResultModel updateDeal(MultilevelActivityBuy multilevelActivityBuy, String shopId, String saleUserId) throws Exception {
		List<MultilevelActivityBuy> buys = multilevelActivityBuyDao.selectMultilevelActivityBuyList(multilevelActivityBuy);
		if(buys.isEmpty()){
			return new ResultModel(ResultCode.RESULT_FAIL, "ID错误");
		}
		Date cDate = this.getDate(0);
		multilevelActivityBuy = buys.get(0);
		String activityId = multilevelActivityBuy.getActivityId();
		String oneShareId = multilevelActivityBuy.getShareId();
		String shareId = multilevelActivityBuy.getShareId();
		MultilevelActivity activity = new MultilevelActivity();
		activity.setActivityId(activityId);
		activity.setActivityState(1);
		List<MultilevelActivity> activities = multilevelActivityDao.selectMultilevelActivityList(activity);
		if(activities.isEmpty()){
			return new ResultModel(ResultCode.RESULT_FAIL, "活动已结束！");
		}
		activity = activities.get(0);
		if(!activity.getShopId().equals(shopId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "车商不于活动不符！");
		}
		if(StringUtils.isNotEmpty(shareId)){
			MultilevelActivityReward reward = new MultilevelActivityReward();
			reward.setRewardTotalLevel(activity.getActivityLevel());
			List<MultilevelActivityReward> rewards = multilevelActivityRewardDao.selectMultilevelActivityRewardList(reward);
			MultilevelActivityShare share = null;
			List<MultilevelActivityShare> shares = null;
			String rewardUserId = null;
			for(MultilevelActivityReward reward1 : rewards){
				share = new MultilevelActivityShare();
				share.setShareId(shareId);
				if(shares == null || oneShareId.equals(shareId)) {
					shares = multilevelActivityShareDao.selectMultilevelActivityShareList(share);
					share = shares.get(0);
					//修改分享状态为已成交
					share.setShareState(3);
					share.setShareUpdateTime(cDate);
					this.update(share);
					shareId = StringUtils.isNotEmpty(share.getShareParentId())?share.getShareParentId():oneShareId;
					rewardUserId = share.getUserId();
				}
				MultilevelActivityDetail detail = new MultilevelActivityDetail();
				detail.setUserId(rewardUserId);
				detail.setDetailMoney(reward1.getRewardRatio().multiply(activity.getActivityBounty()));
				detail.setDetailRemark("分享成交奖励：占比" + reward1.getRewardRatio());
				detail.setDetailCreateTime(cDate);
				//插入奖励明细
				this.insert(detail);
				MultilevelActivityAccount account = new MultilevelActivityAccount();
				account.setUserId(rewardUserId);
				List<MultilevelActivityAccount> accounts = multilevelActivityAccountDao.selectMultilevelActivityAccountList(account);
				if(accounts.isEmpty()){
					account.setAccountBalance(detail.getDetailMoney());
				}else{
					//修改帐户余额
					multilevelActivityAccountDao.updateAddBalance(rewardUserId, detail.getDetailMoney());
				}
			}
		}
		activity.setActivityState(3);
		activity.setActivityUpdateTime(cDate);
		activity.setSaleUserId(saleUserId);
		//修改活动状态
		this.update(activity);
		//购买信息为已成交
		multilevelActivityBuy.setBuyUpdateTime(cDate);
		multilevelActivityBuy.setBuyState(2);
		this.update(multilevelActivityBuy);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "成交", activity);
	}
}
